package com.recorder.ui.home

import android.Manifest
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Toast
import com.github.dfqin.grantor.PermissionListener
import com.github.dfqin.grantor.PermissionsUtil
import com.recorder.R
import com.recorder.ui.card.CardActivity
import com.recorder.ui.face.FaceActivity
import com.recorder.ui.file.FileListActivity
import com.recorder.ui.recorder.RecorderActivity
import kotlinx.android.synthetic.main.activity_first.*


/**
 * Created by YWQ on 2018/9/14.
 * 包名:com.recorder
 * 描述: 程序的入口  显示三个小的入口
 */
class HomeActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_first)


        /**
         * 初始化点击事件
         */

        //首页右上角文件图标 点击事件
        mFileList.setOnClickListener {
            requestPermission(NextActivityType.FILEACTIVITY)
        }

        //录制声纹 点击事件
        mLLRecoder.setOnClickListener {
            requestPermission(NextActivityType.RECORDERACTIVITY)
        }

        //身份证采集 点击事件
        mLLCard.setOnClickListener {
            requestPermission(NextActivityType.CARDACTIVITY)
        }


        //人脸采集

        mLLFace.setOnClickListener {
            requestPermission(NextActivityType.FACEACTIVITY)
        }


    }


    /**
     * 申请权限
     */

    private fun requestPermission(type: NextActivityType) {
        /*
        这里的请求权限可以根据类型来做进一步的优化
         */
        val permissions = arrayOf<String>(Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA)


        if (PermissionsUtil.hasPermission(this@HomeActivity, *permissions)) {
            //已经获取相关权限
            nextActivity(type)
        } else {
            PermissionsUtil.requestPermission(this@HomeActivity, object : PermissionListener {
                override fun permissionGranted(permission: Array<String>) {
                    //用户授予了权限
                    nextActivity(type)
                }

                override fun permissionDenied(permission: Array<String>) {
                    //用户拒绝了权限
                    Toast.makeText(this@HomeActivity, "相关权限被拒绝，本应用将无法正常运行", Toast.LENGTH_SHORT).show()

                }
            }, *permissions)
        }
    }

    /**
     * 根据枚举类型进入下一个界面
     */
    private fun nextActivity(type: NextActivityType) {
        when (type) {
            NextActivityType.FILEACTIVITY -> startFileList()
            NextActivityType.RECORDERACTIVITY -> startRecoder()
            NextActivityType.CARDACTIVITY -> startCard()
            NextActivityType.FACEACTIVITY -> startFace()
        }
    }

    private fun startFileList() {
        val intent = Intent(this, FileListActivity::class.java)
        startActivity(intent)
    }

    private fun startRecoder() {
        val intent = Intent(this, RecorderActivity::class.java)
        startActivity(intent)
    }

    private fun startFace() {
           val intent = Intent(this, FaceActivity::class.java)
            startActivity(intent)
    }

    private fun startCard() {
        val intent = Intent(this, CardActivity::class.java)
        startActivity(intent)
    }

}